
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Xml;
using System.Collections;
using Swaf.Container;

namespace Swaf.Messaging
{
	/// <summary>
	/// Summary description for EmailMsg.
	/// </summary>
	public class EmailMsg : IMessage
	{
		private EmailMessage m_message; 
		private string m_messageBody="";
		private IMessagingConnectionInfo m_connection=null;
		private static string CONNECTION_HEADERBEGIN="<EmailConnection>";
		private static string CONNECTION_HEADEREND="</EmailConnection>";
		private string m_encrypt="none";
		private string m_messageSource="";
		private string m_rawMessageText="";
		/// <summary>
		/// Default constructor
		/// </summary>
		public EmailMsg() {}
		/// <summary>
		/// Constructor for the email message and the message body
		/// </summary>
		public EmailMsg(EmailMessage newMessage, string messageBody)
		{
			m_message = newMessage;
			m_messageBody = MessagingSystemBase.CheckForEncrypt(messageBody,m_encrypt);
			m_rawMessageText=messageBody;
		}
		/// <summary>
		/// Constructor for the email message
		/// </summary>
		public EmailMsg(EmailMessage newMessage)
		{
			m_message = newMessage;
			m_rawMessageText=m_message.Body;
			m_messageBody = MessagingSystemBase.CheckForEncrypt(m_message.Body, m_encrypt);
			if (m_messageBody.StartsWith(CONNECTION_HEADERBEGIN))
			{
				int lastIndex = m_messageBody.IndexOf(CONNECTION_HEADEREND,0) + CONNECTION_HEADEREND.Length;
				createConnection(m_messageBody.Substring(0,lastIndex));
				m_messageBody = m_messageBody.Substring(lastIndex);
			}
		}
		public string RawMessageText
		{
			get
			{
				return m_rawMessageText;
			}
		}

		/// <summary>
		/// The message properties
		/// </summary>
		public IList MessageProperties
		{
			get
			{
				return Cmn.getProperties(m_message);
			}
			set
			{
				Cmn.setProperties(m_message,value);
			}
		}
		public object getRawProperty(string propertyName)
		{
			return Cmn.callPropertyGet(m_message, propertyName);
		}

		public void setRawProperty(string propertyName, object val)
		{
			Cmn.callPropertySet(m_message, propertyName, val);
		}

		/// <summary>
		/// Overload for ToString() to return the message body
		/// </summary>
		public override string ToString()
		{
			return m_messageBody ;
		}
		/// <summary>
		/// Denote is message has a reply
		/// </summary>
		public bool HasReply()
		{
			if( m_message.ReplyTo.Length > 0)
				return true;
			else
				return false;
		}
		/// <summary>
		/// Get the message destination
		/// </summary>
		public string GetDestination()
		{
			return m_message.To;
		}
		/// <summary>
		/// Get the queue manager
		/// </summary>
		public string GetQueueManager()
		{
			return m_message.HostName;
		}
		/// <summary>
		/// Get the correlation id
		/// </summary>
		public object GetCorrelationID()
		{
			return null;
		}
		/// <summary>
		/// Get the message
		/// </summary>
		public object GetMessage()
		{
			return m_message;
		}
		/// <summary>
		/// Create the connection
		/// </summary>	
		private void createConnection(string connection)
		{
			if ((connection==null) || (connection.Equals("")))
				m_connection=null;
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(connection);

			m_connection = new QueueConnectionInfo(doc.DocumentElement);

		}
		/// <summary>
		/// Get the connection information
		/// </summary>
		public IMessagingConnectionInfo GetConnectionInfo()
		{
			return m_connection;
		}
		/// <summary>
		/// Get the encrypted information
		/// </summary>
		public string EncryptedInfo()
		{
			return m_encrypt;
		}
		public string MessageText
		{
			get
			{
				return m_messageBody;
			}	
			set
			{
				m_messageBody = value;
			}
		}
		public string MessageSource
		{
			get
			{
				return m_messageSource;
			}	
			set
			{
				m_messageSource = value;
			}
	
		}
	}
}
